package com.tqfframe.task.learning.threadStudy;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: 唐祺峰
 * @Date: 2020/9/9 0009 14:54
 * @Version 1.0
 */
public class SecondThread {

//    private static  ReentrantLock lock = new ReentrantLock();
    private static Lock lock = new ReentrantLock();
//    static Condition condition = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        TestThread thread=new TestThread();
//        lock.lock();
        new Thread( thread,"t1").start();
        new Thread( thread,"t2").start();
//        condition.await();
//        lock.unlock();
        System.out.println("我执行完了");
    }

    static class TestThread implements Runnable{

        @Override
        public void run() {
            foo();
            too();
        }
    }


    public static void foo(){
        lock.lock();
        int i = 5;
        while( i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " foo: " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
            }
        }
    }

    public static void too(){
        int i = 5;
        while( i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " too: " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
            }
        }
//        condition.signal();
        lock.unlock();
    }

}
